<template>
  <div class="login-account">
    <el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="mobile">
        <el-input v-model="account.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { ElForm } from 'element-plus'
import { rules } from '../config/account-config'
import localCache from '@/utils/cache'
import { useStore } from 'vuex'
export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      mobile:
        (localCache.getCache('accountData') &&
          localCache.getCache('accountData').mobile) ??
        '',
      password:
        (localCache.getCache('accountData') &&
          localCache.getCache('accountData').password) ??
        ''
    })
    //获取el-form的类型
    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          // 1.判断是否记住密码
          if (isKeepPassword) {
            localCache.setCache('accountData', account)
          } else {
            localCache.deleteCache('accountData')
          }
          // 2.登录验证
          store.dispatch('login/accountLoginAction', { ...account })
          // this.$route.path('/')
          console.log('验证通过')
        } else {
          return false
        }
      })
    }
    return {
      account,
      rules,
      loginAction,
      formRef
    }
    // const account = reactive({
    //   name: '',
    //   password: ''
    // })
    // const formRef = ref<InstanceType<typeof ElForm>>()
    // const loginAction = () => {
    //   formRef.value?.validate((valid) => {
    //     if (valid) {
    //       console.log('真正执行登录逻辑')
    //     }
    //   })
    // }
    // return {
    //   account,
    //   rules,
    //   loginAction,
    //   formRef
    // }
  }
})
</script>

<style scoped></style>
